using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Configuration;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string currentFile = System.IO.Path.GetFileName(HttpContext.Current.Request.Url.AbsolutePath).ToLowerInvariant();
        if(currentFile != "install.aspx" && (!WebConfigurationManager.AppSettings.AllKeys.Contains("salt") || WebConfigurationManager.AppSettings["salt"].Length == 0)) {
            Response.Redirect("~/Install.aspx");
        }
    }
    /// <summary>
    /// Outputs a JavaScript array with server and client IDs and a GetClientId method which allows obtaining a client ID from a server ID
    /// </summary>
    /// <param name="wc">The wc.</param>
    public void RenderJSArrayWithCliendIds(params Control[] wc) {
        if(wc.Length > 0) {
            StringBuilder arrClientIDValue = new StringBuilder();
            StringBuilder arrServerIDValue = new StringBuilder();
            ClientScriptManager cs = Page.ClientScript;
            for(int i = 0; i < wc.Length; i++) {
                arrClientIDValue.Append("\"" + wc[i].ClientID + "\",");
                arrServerIDValue.Append("\"" + wc[i].ID + "\",");
            }
            // Register the array of client and server id to the client
            cs.RegisterArrayDeclaration("clientIDS", arrClientIDValue.ToString().Remove(arrClientIDValue.ToString().Length - 1, 1));
            cs.RegisterArrayDeclaration("serverIDs", arrServerIDValue.ToString().Remove(arrServerIDValue.ToString().Length - 1, 1));
            cs.RegisterStartupScript(this.Page.GetType(), "key", @"
            function GetClientId(serverId) {
                for(i=0; i<serverIDs.length; i++) {
                    if(serverIDs[i] == serverId) {
                        return clientIDS[i];
                    }
                }
            }", true);
        }
    }
}
